import { PNT, setNavBarTitle, showToast, shopUrl, formatLeftTime, getCurrentPageUrl, pushPagePath, formatTime } from "../../../utils/utils";
import { Article} from '../../../utils/apis';
var app = getApp();
Page({
  data: {
    time: [
      {
        id: 1,
        sj: '3G资讯',
      
      },
      {
        id: 2,
        sj: '站内快报',
       
      },
      {
        id: 3,
        sj: '商城公告',
        static: 2,
      },
      {
        id: 4,
        sj: '手机新闻',
       
      },
      {
        id: 5,
        sj: '健康新闻',
     
      },
    ],
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    expertList: [
      [
        {
         
          title: "800万像素超强拍照机 LG Viewty Smart再曝光800万像素超强拍照机 LG Viewty Smart再曝光",
          author: '张三',
          add_time: '2019-10-10',
          
        },
        {

          title: "800万像素超强拍照机 LG Viewty Smart再曝光800万像素超强拍照机 LG Viewty Smart再曝光",
          author: '张三',
          add_time: '2019 - 10 - 10',

        },

      ],
      [
        {

          title: "800万像素超强拍照机 LG Viewty Smart再曝光800万像素超强拍照机 LG Viewty Smart再曝光",
          author: '张三',
          add_time: '2019 - 10 - 10',

        },
      ],
    ],
    catid:0,
    page:0,
    flag:true,
    article:[],
  },
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.currentTarget.dataset.current;

    if (this.data.currentTab == cur) { return false; }
    else {
      this.setData({
        currentTab: cur,
        page:0,
        article:[],
      })
      this.article();
    }

  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  onLoad: function () {
    var that = this;
    // 高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
    this.article();
  },

  tiaozhuan: function (e) {
    wx.navigateTo({
      url: '/pages/message/detail/detail?id='+e.currentTarget.dataset.id,
    })
  },
  article(){
    if (!this.data.flag){
      return;
    }
    this.data.flag = false;
    var data = {};
    var self = this;
    data.catid = this.data.currentTab;
    data.page = this.data.page;
    Article(data).then(res=>{
      this.data.flag = true;
      if(data.catid){
        var article = this.data.article;
        res.data.article.forEach(res=>{
          article.push(res);
        })
        self.setData({
          article:article,
        })
      }else{
        self.setData({
          cat:res.data.cat,
          article:res.data.article,
          currentTab:res.data.cat[0].cat_id,
        })
      }
    })
  },
  onReachBottom: function () {
    if(!this.data.flag){
      return;
    }
    var page = this.data.page;
    this.data.page = page + 1;
    this.article();
  }
})